package com.faithbj.printer;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.faithbj.printer.service.LoginService;
import com.faithbj.printer.util.Constants;
import com.faithbj.printer.util.FileUtil;
import com.faithbj.printer.util.KeyUtils;
import com.faithbj.printer.util.PropertiesUtil;

/**
 * Created by IntelliJ IDEA.
 * User: xaoc
 * Date: 30.11.11
 * Time: 17:20
 */
public class Main {
    public static void main(String[] args) throws TimeoutException
    {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
				"classpath:/META-INF/application-context-root.xml");
        
        LoginService loginService = ctx.getBean("loginService", LoginService.class);
        Constants constants = ctx.getBean("constants", Constants.class);
        
        System.setProperty("webdriver.ie.driver",
				"D:\\printer\\IEDriverServer.exe");
		DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
		ieCapabilities.setCapability(
						InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
						true);
		ieCapabilities.setCapability("ignoreZoomSetting", true);
		InternetExplorerDriver driver = new InternetExplorerDriver(ieCapabilities);
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		boolean isSuccess=false;
		
		List<String> trans=FileUtil.readFileToList(constants.getTransactionNumber());
		List<String> steps = FileUtil.readFileToList(constants.getSteps());//读取执行步骤
		List<String> policies = FileUtil.readFileToList(constants.getInsurancePolicy());//生成的单号
		
		
		//先重新登录
		driver.get(constants.getBaseurl() + "/prpall/");
		KeyUtils.sleepMe(2000);
		if(steps.contains("1") ||steps.contains("2")||steps.contains("3")||steps.contains("4")||steps.contains("5")){
			isSuccess = loginService.login(driver);
		}
		
		int step=0;//产生的两个交易号
		for(String bizNo:trans){
	    	String garantee=null;//交强险保单号
	    	String business=null;//商业险保单号
	    	
	    	//确认保单号，记录在policy文件中
			if(steps.contains("1"))
			{
				isSuccess = loginService.queryGuarantee(driver, bizNo);
				isSuccess = loginService.confirmGuarantee(driver);
				isSuccess = loginService.confirmResult(driver);
			}
			
			if(steps.contains("2") ||steps.contains("3")||steps.contains("4")||steps.contains("5"))
			{
				isSuccess = loginService.queryGuarantee(driver, bizNo);
				loginService.checkResult(driver, isSuccess);
			}
			
			if(steps.contains("p"))
			{
				String [] res=policies.get(step).split(",");
				if(res.length==2)
					Constants.data=new String[]{res[0],res[1]};
				else if(res.length==1)
					Constants.data=new String[]{res[0]};
				step++;
			}
			
			//1 单证打印
	        if(steps.contains("2") ){
	        	for(int i=0;i<Constants.data.length;i++){
	        		if(Constants.data[i].substring(8,9).equals("9"))
	        		{
	        			business=Constants.data[i];
	        			break;
	        		}
	        	}
	        	if(business!=null){
	        		isSuccess = loginService.printingPoolQuery(driver, business);
	        		loginService.checkResult(driver, isSuccess);
	        		isSuccess = loginService.certificatePrint(driver);
	        		loginService.checkResult(driver, isSuccess);
	        	}
	        }
	        //交强险打印
	        if(steps.contains("3")   ){
	        	for(int i=0;i<Constants.data.length;i++){
	        		if(Constants.data[i].substring(8,9).equals("7"))
	        		{
	        			garantee=Constants.data[i];
	        			break;
	        		}
	        	}	        	
	        	if(garantee!=null){
		        	isSuccess = loginService.printingPoolQuery(driver,garantee);
		        	loginService.checkResult(driver, isSuccess);
		        	
			        isSuccess = loginService.mandatoryInsurancePrint(driver);
			        loginService.checkResult(driver, isSuccess);
	        	}
	        }
	        //交强险 标志 打印
	        if(steps.contains("4")){
	        	for(int i=0;i<Constants.data.length;i++){
	        		if(Constants.data[i].substring(8,9).equals("7"))
	        		{
	        			garantee=Constants.data[i];
	        			break;
	        		}
	        	}
	        	if(garantee!=null){
	        		isSuccess = loginService.printingPoolQuery(driver,garantee);
	        		loginService.checkResult(driver, isSuccess);
	        		
	        		isSuccess=loginService.insuranceFlagPrint(driver);
	        		loginService.checkResult(driver, isSuccess);
	        	}
	        }
	        
	        //发票打印
	        if(steps.contains("5")){
	        	for(int i=0;i<Constants.data.length;i++){
	        		if(Constants.data[i].substring(8,9).equals("9"))
	        		{
	    				isSuccess = loginService.invoiceQuery(driver,Constants.data[i],1);
	    				loginService.checkResult(driver, isSuccess);
	        		}
	        		else if(Constants.data[i].substring(8,9).equals("7"))
	        		{
	        			isSuccess = loginService.invoiceQuery(driver,Constants.data[i],0);
	        			loginService.checkResult(driver, isSuccess);
	        		}
	        	}	  	        	
	        }
	        
	        if(steps.contains("6")){
		        //打印Excel
	        	if(steps.contains("c")){//说明是没有打印前面的1-5步，直接打印告知书
	        		isSuccess = loginService.printExcel(Constants.data);
	        	}
	        	else//打印了前面的5步后，执行，要减掉流水号
	        	{
	        		int docPrintCount=0;//商业险的数量
	        		int compulsoryCount=0;//交强险的数量
	        		for(String policy:policies){
	        			String [] singleP=policy.split(",");
	        			if(singleP.length==1){//one slip
	        				if(singleP[0].substring(8,9).equals("9")){
	        					docPrintCount+=1;
	        				}
	        				else{
	        					compulsoryCount+=1;
	        				}
	        			}
	        			else{// two slips
	        				compulsoryCount+=1;
	        			}
	        		}
	        		//商业险减docPrintCount
    				String docStr=PropertiesUtil.readValue(Constants.DOC_PRINT_SERIAL);
    				int docSerial=Integer.parseInt(docStr);//流水号-docPrintCount
    				docSerial=docSerial-docPrintCount;
    				PropertiesUtil.writeProperties(Constants.DOC_PRINT_SERIAL, String.valueOf(docSerial));//写回配置
	        		
    				String compStr=PropertiesUtil.readValue(Constants.COMPULSORY_INSURANCE_SERIAL);
    				int compSerial=Integer.parseInt(compStr);//流水号+1
    				compSerial=compSerial-compulsoryCount;
    				PropertiesUtil.writeProperties(Constants.COMPULSORY_INSURANCE_SERIAL, String.valueOf(compSerial));//写回配置文件	        		
	        		
	        		isSuccess = loginService.printExcel(Constants.data);
	        	}
	        	loginService.checkResult(driver, isSuccess);	
			}
		}
		
		
		List<String> accidents=FileUtil.readFileToList(constants.getAccidentTransNumber());
		if(steps.contains("7") || steps.contains("8")){
			isSuccess = loginService.accidentLogin(driver);
			
			for(String bizNo:accidents){
				if(steps.contains("7")){
					isSuccess = loginService.accidentQuery(driver, bizNo,null);
					loginService.checkResult(driver, isSuccess);
				}
				if(steps.contains("8")){
					isSuccess = loginService.accidentInvoicePrint(driver,bizNo,null);
					loginService.checkResult(driver, isSuccess);
				}
			}
		}
        driver.quit();
	    ctx.close();
    }
    
    
}
